package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.MenuDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.pojo.Menu;
import com.itheima.health.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;
/**
 * @Author xuxu
 * @Date 2020/4/15
 * @Version v1.0
 * @Description 菜单服务层接口实现类
 */
@Service
@Transactional
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuDao menuDao;

    //查询菜单
    @Override
    public List<Menu> findAll() {
        return menuDao.findAll();
    }

    //删除
    @Override
    public void deleteById(Integer id) {
        Long count = menuDao.countById(id);
        if (count >0){
            throw new RuntimeException("该菜单被占用, 不能被删除");
        }
        menuDao.deleteById(id);
    }

    //条件查询
    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<Menu> pageResult = menuDao.pageQuery(queryPageBean);
        return new PageResult(pageResult.getTotal(),pageResult.getResult());
    }

    //根据id查对应的菜单
    @Override
    public Menu findById(Integer id) {
        return menuDao.findById(id);
    }

    //编辑菜单
    @Override
    public void edit(Menu menu) {
        menuDao.updateMenu(menu);
    }

    //新增菜单
    @Override
    public void add(Menu menu) {
        menuDao.add(menu);
    }
}
